

$(function() {	
	//初始化表单校验
	initValidate();
	
	$('#postRank').select2({ placeholder: '岗位层级'});
	$('#authority').select2({ placeholder: '数据权限类型'});
	$('#isAuth').select2({ placeholder: '是否根据岗位人员赋权'});
	$('#isConf').select2({ placeholder: '是否可在页面配置'});
	$('#property').select2({ placeholder: '岗位属性'});
	// 属性是否展示
	propertyChange($('#property'));
});

/**
 * 本模块业务校验：验证岗位名称和岗位代码是否已经存在
 */
jQuery.validator.methods["checkIsExists"]=(function(value,element){
	
	//验证是否重复，pass为false代表重复，校验不通过
	var pass = false;
	var postId = $("#postId").val();
	var postTitle = null;
	var postCode = null;
	if(postId == null && postId == ''){
		postId = null;
	}
	if(element.id == "postTitle"){
		postTitle = element.value;
	}else if(element.id == "postCode"){
		postCode = element.value;
	}
	$.ajax({
		  url: PATH_PREFIX+"checkRepeatPost.do",
		  dataType: "json",
		  type: "POST",
		  cache: false,
		  async: false,
		  data: {
			  postCode : postCode,
			  postTitle : postTitle,
			  postId : postId
		  },
		  success: function( data ) {
			  	//console.log(data.object);
				if(data.object == true){
					pass = false;
					return;
				}
				pass=true;
		  },
		  error: function(jqXHR, textStatus, errorThrown){
				ctools.alert("查询失败" + textStatus);
				pass = false;
		  }
	});
	return this.optional(element) || pass;
});

/**
 *  岗位代码 正则表达式
 */
jQuery.validator.methods["checkExp"]=(function(value,element){
	
	//  不支持中文和数字 
	var exp = /^[^\u4e00-\u9fa50-9]+$/;
	// 只支持英文
	//var exp = /^[a-zA-Z]+$/;
	//console.log(exp.test(value));
	return this.optional(element) || exp.test(value);
});

// 校验
function initValidate(){

	 $("#postAddForm").validate({
	        focusCleanup: true,
	        rules: {
	        	postTitle:{
	        		required:true,
	        		checkIsExists:true
	        	},
	        	postCode:{
	        		required:true,
	        		checkIsExists:true,
	        		checkExp:true
	        	},
	        	authority:{
	        		required:true 
	        	},
	        	isConf:{
	        		required:true
	        	},
	        	property:{
	        		required:true
	        	},
	        	remark:{
	        		required:false,
	        		maxlength:1000
	        	}
	        },
	        messages: {
	        	postTitle:{
	        		required:"请输入岗位名称",
	        		checkIsExists:"该岗位名称已存在，请重新填写"
	        	},
	        	postCode:{
	        		required:"请输入岗位代码",
	        		checkIsExists:"该岗位代码已存在，请重新填写",
	        		checkExp:"不支持中文和数字"
	        	},
	        	isConf:{
	        		required:"请选择岗位页面配置"
	        	},
	        	property:{
	        		required:"请选择内部/外部岗位"
	        	},
	        	remark:{
	        		maxlength:"最大长度为1000个文字"
	        	}
	        }
	    });
	
}

// 岗位属性
function propertyChange(element){
	var value = $(element).val();
	if(value == 'INNER'){
		$('#post_anthority').show();
		// 添加校验规则
		$('#isAuth').rules("add", {
			required : true, messages:{required:"请选择岗位人员赋权"}
		});
		$('#authority').rules("add", {
			required : true, messages:{required:"请选择岗位数据权限类型"}
		});
	}else{
		$('#post_anthority').hide();
		// 清除值
		$('#isAuth').val("");
		$("#isAuth").trigger('change.select2');
		$('#authority').val("");
		$("#authority").trigger('change.select2');
		//console.log($('#isAuth'));
		// 删除校验规则
		$('#isAuth').rules('remove','required');
		//清空校验提示语
		$("#isAuth").parent().find('label.error').hide().html('');
		$('#authority').rules('remove','required');
		$("#authority").parent().find('label.error').hide().html('');
	}
}


function submitAddForm(){	
	SubmitAndPreventSecond("postAddForm",function(){
		var postId = $("#postId").val();
		var path = "savePost.do";
		if(postId != null && postId.length > 0){
			path = "updatePost.do";
		}
		$("#postAddForm").attr("action",PATH_PREFIX+path);
	});//防止表单二次提交
}

